package com.yunwaikeji.gy_tool_library.utils;

import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * 键盘工具
 */
public class GYKeyboardUtils {
    private static GYKeyboardUtils gyKeyboardUtils;

    public static GYKeyboardUtils getInstance() {
        if (gyKeyboardUtils == null) {
            gyKeyboardUtils = new GYKeyboardUtils();
        }
        return gyKeyboardUtils;
    }

    private long SHOW_SPACE = 200L;
    private View lastView = null;

    /**
     * 点击其它区域隐藏软键盘
     */
    public boolean setOnOutsideHideSoftInput(Context context, MotionEvent motionEvent) {
        return setOnOutsideHideSoftInput(context, motionEvent, true);
    }

    public boolean setOnOutsideHideSoftInput(Context context, MotionEvent motionEvent, boolean loseFocus) {
        Activity activity = GYUtils.scanForActivity(context);
        if (activity == null) {
            return activity.onTouchEvent(motionEvent);
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            View focusView = activity.getCurrentFocus();
            if (lastView != focusView) {
                hideSoftInput(context, loseFocus);
            }
            lastView = focusView;
            if (focusView == null) {
                hideSoftInput(context, loseFocus);
            } else {
                int[] locations = {0, 0};
                focusView.getLocationInWindow(locations);
                int left = locations[0];
                int top = locations[1];
                int right = left + focusView.getWidth();
                int bottom = top + focusView.getHeight();
                float x = motionEvent.getX();
                float y = motionEvent.getY();
                if (x < left || x > right || y < top || y > bottom) {
                    hideSoftInput(context, loseFocus);
                }
            }
//            return activity.super.dispatchTouchEvent(motionEvent);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (activity.getWindow().superDispatchTouchEvent(motionEvent)) {
            return true;
        }
        return activity.onTouchEvent(motionEvent);
    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftInput(Context context) {
        hideSoftInput(context, true);
    }

    public void hideSoftInput(Context context, boolean loseFocus) {
        Activity activity = GYUtils.scanForActivity(context);
        if (activity == null) {
            return;
        }
        hideSoftInput(activity.getWindow().getDecorView(), loseFocus);
    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftInput(View view) {
        hideSoftInput(view, true);
    }

    public void hideSoftInput(View view, boolean loseFocus) {
        if (view == null || view.getContext() == null) {
            return;
        }
        if (loseFocus) {
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            view.requestFocus();
        }
        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 显示软键盘
     */
    public void showSoftInput(View focusView) {
        if (focusView == null || focusView.getContext() == null) return;
        final InputMethodManager imm = (InputMethodManager) focusView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        focusView.requestFocus();
        focusView.postDelayed(new Runnable() {
            @Override
            public void run() {
                imm.showSoftInput(focusView, InputMethodManager.SHOW_FORCED);
            }
        }, SHOW_SPACE);
    }
}
